<?php

namespace App\Admin\Controllers;
use App\Admin\Extensions\Tools\SynMsgTask;
use App\Admin\Extensions\Tools\SynTask;
use App\Handlers\RedisHandler;
use App\Models\MsgDevice;
use App\Models\MsgTask;
use App\Models\MsgTaskLog;
use App\Models\RateType;
use App\Models\Task;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Table;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Input;
//任务记录日志
class MsgTaskLogController extends Controller
{
    use ModelForm;
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('任务日志');
            $content->description('系统每5分钟自动同步数据，如需立即同步，请点击 同步数据');
            $content->body($this->grid());
        });
    }


    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('任务日志');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });

    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */

    protected function grid()
    {
        return Admin::grid(MsgTaskLog::class, function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->id('ID')->sortable();
            $grid->getMsgTask()->name('任务名');
            $grid->column('deviced_sn', '设备sn');
            $grid->column('account_email', '收信人email');
            $grid->column('msg_device_name', '机器名字');
            $grid->column('msg_device_version', '机器版本');
            $grid->column('log', '失败日志');
            $grid->column('status', '状态')->sortable()->display(function ($value) {
                return $value == 0 ? "成功" : '失败';
            });
            $grid->updated_at('创建时间')->sortable();
            $grid->disableActions();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('deviced_sn', '设备sn');
                $filter->equal('account_email', '收信人email');
                $filter->equal('msg_device_name', '设备')->select(function () {
                    return MsgDevice::all()->pluck('msg_device_name', 'msg_device_name');
                });
                $filter->equal('msg_device_version', '机器版本');
                $filter->between('created_at', '创建时间')->date();

            });
            $grid->tools(function ($tool) {
                $tool->append(new SynTask());
            });
        });
    }

    public function sync()
    {
        RedisHandler::syncMsgLog();
        return ['status_code' => 200, 'message' => '同步成功'];
    }
}

